#include "includes.h"



int momap_encode_file(char *path, MOMAP_PS pstMap, int len, mxml_save_cb_t cb)
{
	FILE *fp;
	int i;
	mxml_node_t *pstHead, *pstElement, *pstNode;
	pstHead = mxmlNewXML("1.0");
	pstElement = mxmlNewElement(pstHead, "xml");
	for (i=0; i<len; i++)
	{
		pstNode = mxmlNewElement(pstElement, pstMap[i].index);
		mxmlNewText(pstNode, 0, pstMap[i].addr);
	}
	fp = Fopen(path, "w");
	mxmlSaveFile(pstHead, fp, cb);
	Fclose(fp);
	mxmlDelete(pstHead);
	return 0;
}

int momap_decode_node(mxml_node_t *node, mxml_node_t *top, MOMAP_PS pstMap, int len)
{
	int i;
	mxml_node_t *pstElement;
	const char *pcName, *pcValue;
	
	for (i=0; i<len; i++)
	{
		pstMap[i].flag = MOMAP_ISCLEAN;
	}
	
	pstElement = node;

	while ((pstElement=mxmlWalkNext(pstElement, top, MXML_DESCEND)) != NULL)
	{
		if ((pcName = mxmlGetElement(pstElement))==NULL || (pcValue = mxmlGetText(pstElement, 0))==NULL)
			continue;
		for (i=0; i<len; i++)
		{
			if (pstMap[i].flag == MOMAP_ISCLEAN && !strcmp(pcName, pstMap[i].index))
			{
				strncpy(pstMap[i].addr, pcValue, pstMap[i].len);
				pstMap[i].flag = MOMAP_ISDIRTY;
			}
		}

	}

	for (i=0; i<len; i++)
	{
		if (pstMap[i].flag == MOMAP_ISCLEAN)
		{
			err_sys("can not find the %s", pstMap[i].index);
			return -1;
		}
	}
	return 0;
}

